$(function(){
		//引用插件
		$("#register").validate({
			rules: {
				firstname: "required",
      			lastname: "required",
				username: {
					required: true,
					rangelength:[4,20]
				},
				password: {
					required: true,
					rangelength:[6,16]
				},
				confirm_password: {
					required: true,
					rangelength:[6,16],
					equalTo: "#password"
				},
				email: {
					required: true,
					email: true
				},
				look: {
					required: "#look:checked",
				},
				
				agree: "required"
			},
			errorPlacement: function(error, element) {  
    			error.appendTo(element.closest("td").next());  
			},
			messages: {
				firstname: "Please enter your firstname",
				lastname: "Please enter your lastname",
				username: {
					required: "用户名不能为空",
					rangelength: "用户名长度必须在4-20位之间"
				},
				password: {
					required: "密码不能为空",
					rangelength: "密码长度必须在6-16位之间"
				},
				confirm_password: {
					required: "请再次输入密码",
					rangelength: "密码长度必须在6-16位之间",
					equalTo: "两次输入的密码不符"
				},
				email: "请正确输入邮箱地址",
				look: "请认真阅读飞虎用户协议"
			},
			
		});
		
		var str = "";//生成验证码
		check_code()
		function check_code(){
			//随机四位数
				str ="";
				var time = 60 ;
				for(i=0;i<4;i++){
					num = parseInt(Math.random()*62);
					if(num<=9){
						str=str+num;
					}else if(num>=10&&num<36){
						str = str + String.fromCharCode(num+55);
					}else if(num>=36&&num<=62){
						str = str + String.fromCharCode(num+61)
					}	
				}
			color1 = 'rgb('+parseInt(Math.random()*255)+','+parseInt(Math.random()*255)+','+parseInt(Math.random()*255)+')'
			color2 = 'rgb('+parseInt(Math.random()*255)+','+parseInt(Math.random()*255)+','+parseInt(Math.random()*255)+')'
			//验证码随机背景色
			$("#check_code").css("background","-webkit-gradient(linear, 50% 10%, 0% 100%,from("+color1+"), to("+color2+"))").html(str)
			$("#check_code").css("background","-moz-linear-gradient(top,left,"+color1+","+color2+")").html(str)//背景颜色渐变
		}
		
		$("#change_code").click(function(){ //更换验证码
			check_code()
		})
		console.log(str)
		$("#form_sub").click(function(){
			if($("#se_code").val()==str){
				var d = new Date();
						d.setDate(d.getDate()+10);
					var	obj = {
							"username":$("#username").val(),
							"password":$("#password").val(),
							"email":$("#email").val()
					}	
				if(getCookie("user")!=""){
					var	cook = getCookie("user")
					var	user = JSON.parse(cook)	
					/*for(var i = 0;i<user.data.length;i++){
						console.log(user.data[i].username)
						if(obj.username == user.data[i].username){
							alert("用户名已存在，请重新输入")
							break;
						}else {
							user.data.push(obj)
							var	user = JSON.stringify(user)
							setCookie("user",user,d);
							alert("恭喜您注册成功，您的账户名："+obj.username+"请注意保存")
							window.location.href = "login.html"
						}
					}*/
					var attr = [] //生成数组判断用户是否存在
					for(var i = 0;i<user.data.length;i++){
						attr.push(user.data[i].username)
					}
					console.log($.inArray(obj.username,attr))
					if($.inArray(obj.username,attr)!=-1){
						alert("用户名已存在，请重新输入")
					}else{
						user.data.push(obj)
						var	user = JSON.stringify(user)
						setCookie("user",user,d);
						alert("恭喜您注册成功，您的账户名："+obj.username+"请注意保存")
						window.location.href = "login.html"
					}	
				}else{
					var user = {data:[]};
						user.data.push(obj)
					var	user = JSON.stringify(user)
						setCookie("user",user,d);
					alert("恭喜您注册成功，您的账户名："+obj.username+"请注意保存")
						window.location.href = "login.html"
				}
			}else{
				alert("请正确输入验证码")
			}
		})
})
